<?php

 require_once 'Zend/Pdf.php';
 require_once 'Zend/Loader/Autoloader.php';
 require_once 'Zend/Pdf/Style.php';
 require_once 'My/Pdf.php';
 require_once 'My/Pdf/Table.php';
 require_once 'My/Pdf/Document.php';
 require_once 'My/Pdf/Page.php';
 require_once 'My/Pdf/Table/Row.php';
 require_once 'My/Pdf/Table/Column.php';
 require_once 'My/Pdf/Table/Cell.php';
 require_once 'My/Pdf/Table/HeaderRow.php';
 require_once APPLICATION_PATH.'\misc\sessionController.php';
 // register auto-loader



 //require_once LIBRARY_PATH.'\Zend\PDF.php';

class PrintPDFController extends Zend_Controller_Action
{
  private $item_per_page;

    public function init()
    {
        $sessionController=new sessionController();
        $this->view->sessioncontroller = $sessionController;
    }

    public function indexAction()
    {
        // action body
    }

    public function createpdfAction(){

       // register auto-loader
        $loader = Zend_Loader_Autoloader::getInstance();

    try {

        // configure adapter and query database
          //$db = Zend_Db::factory('PDO_MYSQL', $params);
          $db = new Application_Model_DbTable_Shop();

          // parameter for database query
            $name = $this->getRequest()->getParam("name");
            $order = $this->getRequest()->getParam("order","name");
            $method = $this->getRequest()->getParam("method","asc");
            $pageform = $this->getRequest()->getParam("page",1);
            $this->view->page = $pageform;
            $this->view->order = $order;
            $this->view->method = $method;

        // Load approved shop from database
        $this->view->message = $this->_helper->_flashMessenger->getMessages();

        $printshop_db = new Application_Model_DbTable_Shop();
        if($name!=null || strlen($name)!=0){
            $shops = $printshop_db->pdfToko($this->item_per_page,$page,"status = 1 AND name like '%$name%'","name");
        }else{
            $shops = $printshop_db->pdfToko($this->item_per_page,$pageform,"status = 1",$order,$method);
        }
        $this->view->shops = $shops;


       //Load data from table shop, category, and keyword into an array
        $print_shop = array();

        $print_shop[0][] = "Nama Toko";
        $print_shop[0][] = "Nama Pemilik";
        $print_shop[0][] = "Keanggotaan";
        $print_shop[0][]= "Kategori";
        $print_shop[0][] = "Keyword";


        foreach($shops as $printshop){
            $print_shop [$printshop['shop_id']]['name'] = $printshop['name'];
            $print_shop [$printshop['shop_id']]['owner'] = $printshop['owner'];
            $print_shop [$printshop['shop_id']]['membership']= $printshop['ms_name'];
     
                // List keywords
                //Load keywords
                $keys = $printshop_db->getKeyword($printshop["shop_id"]);
                $keyword = array();
                foreach ($keys as $key) {
                    $keyword[] = $key->name;
                }
                
                $print_shop[$printshop['shop_id']]['keywords'] = implode(",",$keyword);


                // Load categories of each shop
               $cats = $printshop_db->getCategory($printshop['shop_id']);

                    // List categories
                 $category = array();
                 foreach ($cats as $cat) {
                         $category[] = $cat->cat_name;
                    }

               $print_shop[$printshop['shop_id']]['categories'] = implode(",",$category);

               

        }
       
          // create PDF
          $pdf = new My_Pdf_Document('DaftarToko.pdf', '.');

         // create A4 page
         $page = new My_Pdf_Page(My_Pdf_Page::SIZE_A4);

        // define font resource
        $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
        $fontTitle = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD);

        // set font for page
        $page->setFont($font, 24);


         // draw  line at the Top of the page
         $page->setLineColor(new Zend_Pdf_Color_Rgb(0,0,128));
         $page->drawLine(10, 55, ($page->getWidth()-10), 55);

        // define image resource
        $image = Zend_Pdf_Image::imageWithPath('logoBitzPlaza.png');

        // write image to page
        $page->drawImage($image, 25, 7, ($image->getPixelWidth()+20), (7+$image->getPixelHeight()));

         // set the page title for Daftar Toko
         $page->setFont($fontTitle, 10);
         $page->drawText('DAFTAR TOKO', ($page->getWidth()/2)-30, 130);


          // create table
         $table = new My_Pdf_Table(4);

         // create ta
        // $tablecategory = new My_Pdf_Table(1);

  
        // iterate over record set
        // set up table content
         echo "<script>alert('".count($shops)."')</script>";
       foreach ($print_shop as $recordshop ) {
                $row = new My_Pdf_Table_Row();
                $cols = array();
                
                foreach ($recordshop as $k => $v) {
                 $col = new My_Pdf_Table_Column();
                 $col->setText($v);
                 $cols[] = $col;
                }


                 $row->setColumns($cols);
                 $row->setFont($font, 11);
                 $row->setBorder(My_Pdf::TOP, new Zend_Pdf_Style());
                 $row->setBorder(My_Pdf::RIGHT, new Zend_Pdf_Style());
                 $row->setBorder(My_Pdf::BOTTOM, new Zend_Pdf_Style());
                 $row->setBorder(My_Pdf::LEFT, new Zend_Pdf_Style());
                 $row->setCellPaddings(array(5,5,5,5));
                 
                 $table->addRow($row);
          }

          


         // add table to page
         $page->addTable($table, 25, 150);
       //  $page->addTable($tablecategory,50, 150);

        // draw a line at the Bottom of the page
         $page->drawLine(10, 810, ($page->getWidth()-10), 810);

        // add footer text
        $page->drawText('Copyright BitzPlaza 2011. All rights reserved.', ($page->getWidth()/3), 825);

         // add page to document
        $pdf->pages[] = $page;

        // save as file
         $pdf->save('example.pdf');
         echo 'SUCCESS: Document saved!';
         } catch (Zend_Pdf_Exception $e) {
                    die ('PDF error: ' . $e->getMessage());
                    } catch (Exception $e) {
                    die ('Application error: ' . $e->getMessage());
                    }



    }


}

